<?php

require_once("includes/pagetop.php");

if(!(isset($_SESSION['Allyfolio_User_ID']) && trim($_SESSION['Allyfolio_User_ID'])!=""))
{
    redirect("index.php");
    exit();
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>AllyFolio | Search Friend </title>
<link href="css/style.css" rel="stylesheet" type="text/css" media="all"  />
<script src="js/jquery1.10.2.min.js"></script>
<script src="js/jquery.placeholder.js"></script>

<script src="js/general.js"></script>
<script src="js/functions.js"></script>

	

</script>

<script type="text/javascript">
    $(function() { $('input, textarea').placeholder();});
</script>
<style type="text/css">
    .placeholder {
      color: #cccccc;
      }
    .userpiclass
    {
        border:1px solid #999;
    }
    .friendrow
    {
        border-bottom: 1px solid #999;
        height:60px;
    }
</style>

<link rel="stylesheet" href="alertify/themes/alertify.core.css" />
<link rel="stylesheet" href="alertify/themes/alertify.default.css" id="toggleCSS" />
<script src="alertify/lib/alertify.min.js"></script>
    
</head>

<body>

	<div id="main_wrapper">
    <?php
    
    $pageheading="Search Friends";
    require_once("includes/header.php");
    
    $condition=array();
    $query="select User_ID,Allyfolio_User_ID,Profile_Status,Profile_Pic_Path,Full_Name from af_users where Activation_Status='Active' and User_ID!='".$_SESSION['Allyfolio_User_ID']."' and Profile_Status=0 and ( ";
    $searchcond=array();
    
    
    if(isset($_POST['User']['emailid']) && trim($_POST['User']['emailid'])!="")
    {
        $query.=" Email_ID like :emailid or";
        $searchcond[":emailid"]="%".trim($_POST['User']['emailid'])."%";
    }
    if(isset($_POST['User']['username']) && trim($_POST['User']['username'])!="")
    {
        $query.=" Full_Name like :fullname or";
        $searchcond[":fullname"]="%".trim($_POST['User']['username'])."%";
    }
    if(count($searchcond)>0)
        $query=substr($query,0,strlen($query)-2);
        
    $query.=" )";
    
    if(count($searchcond)==0)
        $query=substr($query,0,strlen($query)-8);
        
    $query.=" order by Full_Name asc " ;
    if(count($searchcond)==0)
    {
        $query.=" limit 20";
    }
    
    $friendsinfo=$pdodbobj->fetch_array($pdodbobj->query($query,$searchcond));
    
    
    ?>
    
    <!--=================Body Contain Start=======================-->
    <form name="mainform" method="post">
    <div class="wrapper">
    	<div class="header">
        	<div class="contain-wrap">
            	<div style="width:63%">
                	<p class="dn_txt">Find Your Friends, Colleagues, Family Members, Classmates & Many More ........
                        By Simply Entering His/Her Name or E-Mail Address.</p>
                </div>
                
                <div class="clearfix"></div>
            
            </div>
            
            <div class="clearfix"></div>
        </div>
        <table cellpadding="0" cellspacing="0" style="width: 980px;margin: 0 auto;">
            <tr>
                <td style="width: 300px;vertical-align: top;">
                   <div class="help_btn" style="margin-left: 14px;">
                   	<ul>
                    	<li><p class="dn_txt">Search Friends By E-Mail</p></li>
                        <li>
        				
        				<input type="text" id="emailid" name="User[emailid]" class="enter" placeholder="Enter Your E-Mail" style="padding-left:0; padding-right:0; width:264px; float:left;" value="<?php if(isset($_POST['User']['emailid']) && trim($_POST['User']['emailid'])!=""){ echo $_POST['User']['emailid']; } ?>" />
        				</li>
                        <li style="margin-left:38px;"><a href="#" style="padding-left:0px; padding-right:0px; width:110px; float:left; margin-top:10px; margin-left:35px;" onclick="return getFilteredUsers('emailid','username');">Find</a></li>
                    </ul>
                   </div>
        	       <div class="clearfix" style="margin-bottom:100px;"></div>
        	       <p class="dn_txt" style="margin-left: 18px;">Search Friends</p><br />
        
        		   <div class="search_bar" style="float:left;margin-left: 18px;">
        		   	<span class="carve_left"></span>
                    <span class="search_area" style="width:200px"><input type="text" id="username" name="User[username]" placeholder="Enter Name" value="<?php if(isset($_POST['User']['username']) && trim($_POST['User']['username'])!=""){ echo $_POST['User']['username']; } ?>" class="exz" style="width:200px" /></span>
                    <a href="#" class="search_btn" onclick="return getFilteredUsers('username','emailid');"></a>
                   </div>
                </td>
                <td style="width: 680px;border-left: 2px solid #999;vertical-align: top;">
                    <table style="width: 100%;padding:2px;">
                    <?php
                    if(count($friendsinfo)>0)
                    {
                        for($i=0;$i<count($friendsinfo);$i++)
                        {
                            $requestary="select Unique_User_Friend_ID,User_ID,User_Friend_ID,Status from af_user_friends where (User_ID=:fromuserid && User_Friend_ID='".$_SESSION['Allyfolio_User_ID']."' ) or (User_Friend_ID=:touserid && User_ID='".$_SESSION['Allyfolio_User_ID']."' )";
                            $checkstatus=$pdodbobj->fetch_array($pdodbobj->query($requestary,array("fromuserid"=>$friendsinfo[$i]['User_ID'],"touserid"=>$friendsinfo[$i]['User_ID'])));
                                                    
                        ?>
                            <tr>
                                <td class="friendrow" style="vertical-align: top;padding-bottom:5px;padding-top:5px;">
                                    <table style="width: 100%;">
                                        <tr>
                                            <td style="vertical-align: top;">
                                                <a href="dna.php?t=<?php echo $friendsinfo[$i]['Allyfolio_User_ID']; ?>"> <img class="userpiclass" src="<?php if(trim($friendsinfo[$i]['Profile_Pic_Path'])!=""){ echo $friendsinfo[$i]['Profile_Pic_Path']; }else{ echo "images/default_user_pic.jpg"; } ?>" height="50px" width="50px" /></a>
                                            </td>
                                            <td style="vertical-align: top;">
                                                <p><a href="dna.php?t=<?php echo $friendsinfo[$i]['Allyfolio_User_ID']; ?>"><?php echo $friendsinfo[$i]['Full_Name']; ?></p></a>
                                                <p><?php echo getCommonFriends($pdodbobj,$friendsinfo[$i]['User_ID'],$_SESSION['Allyfolio_User_ID'])." Common Friends"; ?></p>
                                                <span class="btn">
                                                    Friend Request Status : <span id="requeststatus_<?php echo $i; ?>">
                                                    <?php
                                                    if(count($checkstatus)==0)
                                                    {
                                                        ?>
                                                        <input type="button" value="Send Request" onclick="return sendFriendRequest(<?php echo $friendsinfo[$i]['User_ID']; ?>,<?php echo $i; ?>);" />    
                                                        <?php
                                                    }
                                                    else
                                                    {
                                                        if($checkstatus[0]['Status']==1)
                                                        {
                                                            echo "<b>Already a Friend.</b>";
                                                        }
                                                        else if($checkstatus[0]['Status']==0)
                                                        {
                                                            echo "<b>Blocked.</b>";
                                                        }
                                                        else if($checkstatus[0]['Status']==4)
                                                        {
                                                            echo "<b>Rejected.</b>";
                                                        }
                                                        else if($checkstatus[0]['Status']==2)
                                                        {
                                                            echo "<b>Temporary Blocked.</b>";
                                                        }
                                                        else if($checkstatus[0]['Status']==3 && $checkstatus[0]['User_Friend_ID']==$friendsinfo[$i]['User_ID'])
                                                        {
                                                            echo "<b>Friends Request Sent.</b>";
                                                        }
                                                        else if($checkstatus[0]['Status']==3 && $checkstatus[0]['User_ID']==$friendsinfo[$i]['User_ID'])
                                                        {
                                                            ?>
                                                            <input type="button" value="Accept" onclick="return changeStatusFriendRequest(1,<?php echo $checkstatus[0]['Unique_User_Friend_ID']; ?>,<?php echo $i; ?>);" />
                                                			<input type="button" value="Reject"  onclick="return changeStatusFriendRequest(4,<?php echo $checkstatus[0]['Unique_User_Friend_ID']; ?>,<?php echo $i; ?>);" />
                                                      		<input type="button" value="Block"  onclick="return changeStatusFriendRequest(0,<?php echo $checkstatus[0]['Unique_User_Friend_ID']; ?>,<?php echo $i; ?>);" />
                                		  
                                                            <?php
                                                        }
                                                    }
                                                    ?>
                                                        </span>
                                                </span>
                                             </td>
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                        <?php
                        }
                    }
                    else
                    {
                        ?>
                        <tr>
                                <td class="friendrow" style="vertical-align: top;padding-bottom:5px;padding-top:5px;color:red;height:20px;">
                                    No User record found!!!
                                </td>
                            </tr>
                        <?php
                        
                    }
                    ?>
                    </table>
                </td>
            </tr>
        </table>
    </div>
    <div class="clearfix"></div>
    </form>        
            
	</div>
    <div class="clearfix"></div>
    <?php
    require_once("includes/footer.php");
?>
<script lang="javascript">
//<![CDATA[
function sendFriendRequest(touserid,rowid)
{
    alertify.confirm("Are you sure, you want to send friend request?", function (e) {
		if (e) {
			alertify.success("You've clicked OK");
			$.ajax({
              cache:false,  
              async:false,
              url: "ajax_search/ajax_manageRequests.php?action=friendrequest&touserid="+touserid,
        	  success: function( data ) {
        	    if(trim(data)=="success")
                {
                    alertify.alert("You have successfully sent friend request.");
                    document.getElementById('requeststatus_'+rowid).innerHTML="<b>Friends Request Sent.</b>";
                    if(document.getElementById('alertify-cover'))
                    {
                        document.getElementById('alertify-cover').style.display='none';
                    }
                    return false;
                }
                
        	  }
        	});
		} else {
			alertify.error("You've clicked Cancel");
		}
	});
	
    
    
}

var requeststarr=new Array();
requeststarr[1]="Accepted";
requeststarr[0]="Blocked";
requeststarr[4]="Rejected";
function changeStatusFriendRequest(status,recordid,rowid)
{
    if(recordid>0 && document.getElementById('requeststatus_'+rowid))
    {
        $.ajax({
          cache:false,  
          async:false,
          url: "ajax_search/ajax_manageRequests.php?action=changereqstatus&recordid="+recordid+"&status="+status,
    	  success: function( data ) {
    	    if(trim(data)=="success")
            {
                document.getElementById('requeststatus_'+rowid).innerHTML="<b>"+requeststarr[status]+"</b>";
                return false;
            }
            
    	  }
    	});
    }
}

function getFilteredUsers(fieldid,anothfid)
{
    if(trim(document.getElementById(fieldid).value)=="")
    {
        alertify.alert("Please enter "+fieldid);
        return false;
    }
    else
    {
        document.getElementById(anothfid).value="";
        document.mainform.submit();
    }
}
//]]>
</script>